Welcome to Insiders' Viewpoints: JavaScript Best Practices. In this video, we will listen
to several cloud application professionals discuss best practices
from their experiences with JavaScript. Can you share some best practices from your
experiences of working with JavaScript? I want to share three best practices
that I've learned over the years working in this field, especially with JavaScript. So, first is use of IDEs. I love using something
like VS code, or WebStorm to write code. In the content assist I am able to look at functions and documentation and
classes all within the same editor. It is very powerful to me, and it's very
effective, makes me a more productive developer. My second advice would be to use
linters. So, if you're new to JavaScript, and you don't know what linters are,
they help you write Standard Code. Now, I don't mean standard
in terms of ES6 standard. But this could be standard
in terms of how your team decides to write functions and documentations. And this is very important
when it comes to version control and resolving conflicts.
It makes your life a lot easier. And finally, my third tip would be to learn the
different paradigms and patterns in JavaScript. So assuming you're using JavaScript for your
front end, or back end, learn how to modularize code in JavaScript, learn how to structure
your code so it is easier to unit test, structure code so it's easier to import
other packages into your application. So working with JavaScript has
changed a lot over the years. It's actually pretty robust nowadays, and
there are lot of tools you can use for it. I definitely would recommend
linting your JavaScript. One really great tool that I personally use is called XO. It is a great linter
that has some great default set. And you just run that on your code. And it makes you follow specific standard styles so that you and everyone on your team
makes sure that your code looks good, but also hopefully, reduce the amount
of bugs and confusion in your code. I would also recommend using TypeScript. It's a wonderful addition to JavaScript and
often can just be plunked right on top of the current JavaScript project without
needing to change much code at all. And then you can add a whole significant set of
features onto your code to make it much better. While working with JavaScript, avoid
declaring global variables and functions. Global variables can slow down your program since
they are not deleted until the window is closed. Scripts that include same variable and run
after your code will override your variables. In order to reduce page load times,
place your js at the bottom of your page, especially if the purpose of js is to
add functionality after an event occurs. When declaring lengthy ‘for’ statements declare
variables outside of the ‘for’ statement. You should also optimize loops. For example, instead of reading the length
attribute of an array at every iteration, store the length value in a different variable. Always comment important sections of your
code so that you or your colleagues can understand the purpose of the code
when you need to revise your code. So in writing JavaScript, I really like
to take advantage of the features of ES6, because it can make my code look cleaner,
easier to read, and, just more beautiful, such as the arrow functions,
or the dot dot dot operator. And so I encourage you to,
after learning JavaScript, dive into ES6 a bit and have fun with it. One best practice for JavaScript developers to keep in mind is to avoid
the use of global variables. You might have heard this advice for
other programming languages as well, and the reasons there apply here as well. But with JavaScript specifically, the
risk of name collision is quite high. So, it's very common for developers to include their code as well as third
party libraries on the same page. And all of these scripts will share the same
global context, which means if you happen to pick the same name for a global variable
as one of those third-party libraries, basically one or both of
your programs will not work. JavaScript is a very powerful language. It lets you do pretty much
anything you can imagine, including shooting yourself in
the foot if you're not careful. The practices I typically follow with JavaScript
include proper scoping of variables and functions. And writing unit and integration
tests for my features.